// pages/meeting/detail.js
const app = getApp();
var core = require('../../utils/core.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:10,
    data:  {  
    },
    qrcodeFlag:0,
    qrcode_img:'https://weiqing.parkic.com/attachment/images/3/2020/08/M7q7P5bqR7TeMeqPE7e8A3MpiBI7OR.jpg',
    animationData: {},//: true, //模态框的状态  true-隐藏  false-显示
    hideModal:true,

    animationData1: {},//
    hideModal1:true,
    auto_focus:false,

    LoadingFlag:true,
    content:'',
    page:1,
    comment:[],
    share_page:1,
    share_data:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options != null&& options!= undefined&&options.openid) {
      wx.setStorageSync('otheropenid', options.openid)  
    }
      if(options.id){
        this.setData({id:options.id})
      }
      if(options.scene){
        const scene = decodeURIComponent(options.scene)
        this.setData({id:scene})
      }
      this.getInfo();
      this.getCommentList();
      this.getShareList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    var enrollId=wx.getStorageSync('enrollId')
    if(enrollId&&enrollId==this.data.id){
       wx.removeStorageSync('enrollId') 
       this.getInfo()  
       this.getEnrollData();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getInfo()
    this.getEnrollData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var self=this
    self.setData({page:self.data.page*1+1});
    self.getCommentList();
  },
   /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return this.share()
  },
    /**
   * 用户点击右上角分享
   */
  onShareTimeline: function () {
    return this.share()
  },
  share:function(){
    var self=this;
   
    var data={
      id:self.data.id,
      op:'share'
    };
    core.post("localprojects",data,function(res){
      if(res.code==1){
         //self.getCommentList();
         self.hideModal();  
         self.hideModal1(); 
         self.setData({share_page:1,share_data:[]});
         self.getShareList();

         self.setData({
          'data.share_num': (self.data.data.share_num*1+1)
        })
         
      }
    })
    return {
      title:  this.data.data.name,
      path: '/pages/localproject/detail?id=' + this.data.id+'&openid='+wx.getStorageSync('openid1'),
      imageUrl:  ''//自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径。支持PNG及JPG。显示图片长宽比是 5:4。
    }
    
  },
getInfo:function(){
      
    //this.data=this.data1
    wx.showLoading({
      title: '加载中',
    })
    var self=this
    self.setData({LoadingFlag:true})
    var data={
      id:self.data.id,
      op:'detail',
    }
    core.post("LocalProjects",data,function(res){
      if(res.code==1){
        self.setData({data:res.data})
        //WxParse.wxParse('content', 'html', res.data.content, self, 5);
      }
      wx.stopPullDownRefresh()
      wx.hideLoading();
      wx.stopPullDownRefresh()
      setTimeout(function(){
          self.setData({LoadingFlag:false})
      },2000)
    })
    
},
/**
 * 收藏、取消收藏
 */
collect:function(){
  var self=this
  var data={
    id:self.data.id,
    op:'collect'
  }
  core.post("LocalProjects",data,function(res){
    if(res.code==1){
      self.setData({'data.IsCollect':res.data.is_collect,'data.collect_num':res.data.collect_num})
    }
  })
 },
 liuyan:function(){
  wx.navigateTo({
    url:"/pages/localproject/liuyan?id="+this.data.id
  })  
 },
 Location:function(){
  var self=this   
  wx.openLocation({
    name:self.data.data.name,
    address:self.data.data.address,
    latitude:self.data.data.lat*1,
    longitude:self.data.data.lng*1,
    scale: 18
  })
},
getEnrollData:function(){
  var self=this
  var data={
    id:self.data.id,
    op:'EnrollList'
  }
  core.post("Meeting",data,function(res){
    if(res.code==1){
      self.setData({enrolldata:res.data})
    }
  })
},
/**
   * 海报生成
   */
  create_qrcode:function(){
    this.hideModal()
    this.hideModal1()
    wx.showLoading({
      title: '生成中',
    })
    var self=this
    core.post("Qrcode",{id:self.data.id,op:'create',type:4},function(res){
      if(res.code==1){
        self.setData({qrcode_img:res.data.qrcode,qrcodeFlag:1})
      }
      wx.hideLoading();
    })
  },
  /**
   * 海报保存
   */
  save_qrcode:function(){
    var self=this
    wx.getImageInfo({
      src: self.data.qrcode_img,
      success: function (ret) {
          var path = ret.path;
          wx.saveImageToPhotosAlbum({
              filePath: path,
              success(result) {
                wx.showToast({
                  'title':'保存成功',
                  'icon':'success'
                })
              }
          })
      }
    })
  },
  /**
   * 海报取消
   */
  cancel_qrcode:function(){
    this.setData({qrcodeFlag:0})
  },
  saveImage: function (e) {
    console.log(3333)
    wx.getImageInfo({
      src: this.data.data.qcode,
      success: function (ret) {
          var path = ret.path;
          wx.saveImageToPhotosAlbum({
              filePath: path,
              success(result) {
                wx.showToast({
                  'title':'保存成功',
                  'icon':'success'
                })
              }
          })
      }
    })
   },
   // 显示遮罩层
  showModal: function () {
    var that=this;
    that.setData({
      hideModal:false
    })
    var animation = wx.createAnimation({
      duration: 400,//动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
      timingFunction: 'ease',//动画的效果 默认值是linear
    })
    this.animation = animation 
    setTimeout(function(){
      that.fadeIn();//调用显示动画
    },10)   
  },

     // 显示遮罩层
     showModal1: function () {
      var that=this;
      that.setData({
        hideModal1:false
      })
      var animation = wx.createAnimation({
        duration: 400,//动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
        timingFunction: 'ease',//动画的效果 默认值是linear
      })
      this.animation = animation 
      setTimeout(function(){
        that.fadeIn1();//调用显示动画
        that.setData({auto_focus:true})
      },10)   
    },

  // 隐藏遮罩层
  hideModal: function () {
    var that=this; 
    var animation = wx.createAnimation({
      duration: 400,//动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
      timingFunction: 'ease',//动画的效果 默认值是linear
    })
    this.animation = animation
    that.fadeDown();//调用隐藏动画   
    setTimeout(function(){
      that.setData({
        hideModal:true,
      })      
    },10)//先执行下滑动画，再隐藏模块
    
  },
    // 隐藏遮罩层
    hideModal1: function () {
      var that=this; 
      var animation = wx.createAnimation({
        duration: 400,//动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
        timingFunction: 'ease',//动画的效果 默认值是linear
      })
      this.animation = animation
      that.fadeDown1();//调用隐藏动画   
      setTimeout(function(){
        that.setData({
          hideModal1:true,
        })      
      },10)//先执行下滑动画，再隐藏模块
      
    },

  //动画集
  fadeIn:function(){
    this.animation.translateY(0).step()
    this.setData({
      animationData: this.animation.export()//动画实例的export方法导出动画数据传递给组件的animation属性
    })    
  },
    //动画集
  fadeIn1:function(){
      this.animation.translateY(0).step()
      this.setData({
        animationData1: this.animation.export()//动画实例的export方法导出动画数据传递给组件的animation属性
      })    
    },
  fadeDown:function(){
    this.animation.translateY(600).step()
    this.setData({
      animationData: this.animation.export(),  
    })
  }, 
  fadeDown1:function(){
    this.animation.translateY(600).step()
    this.setData({
      animationData1: this.animation.export(),  
    })
  }, 
  getval(e) {
    var name=e.target.id
    this.setData({
      [name]: e.detail.value
    })
  },
  comment:function(){
    var self=this;
    if(self.data.content==''){
       wx.showToast({
         'title':'请输入内容',
         'icon':'none'
       });
       return
    }
    var data={
      id:self.data.id,
      content:self.data.content,
      op:'comment',
    };
    core.post("localprojects",data,function(res){
      if(res.code==1){
         wx.showToast({
           'title':'评论成功',
           'icon':'success',
         })
         self.setData({page:1,comment:[],auto_focus:false});
         self.getCommentList();
         self.setData({
          'data.comment_num': (self.data.data.comment_num*1+1)
        })
        self.hideModal1();   
      }else{
       wx.showToast({
         'title':res.msg,
         'icon':'none'
       });
      }
    })
 },
 getCommentList:function(){
     
   //this.data=this.data1
   wx.showLoading({
     title: '加载中',
   })
   var self=this
   var redata={
     id:self.data.id,
     page:self.data.page,
     op:'CommentList'
   };
   core.post("localprojects",redata,function(res){
    
     var data=self.data.comment
     if(res.code==1){
        for (let i = 0; i < res.data.length; i++) {
            data.push(res.data[i])             
        }
        self.setData({comment:data})
     }
     wx.hideLoading();
   })
   
},
getShareList:function(){
     
  //this.data=this.data1
  wx.showLoading({
    title: '加载中',
  })
  var self=this
  var redata={
    id:self.data.id,
    page:self.data.share_page,
    op:'ShareList',
    limit:6
  };
  core.post("localprojects",redata,function(res){
   
    var data=self.data.share_data
    if(res.code==1){
       for (let i = 0; i < res.data.length; i++) {
           data.push(res.data[i])             
       }
       self.setData({share_data:data,share_page:self.data.share_page+1})
    }
    wx.hideLoading();
  })
  
},
})